package main

import (
	"flag"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/lib/pq"
	"github.com/robfig/cron/v3"
	"jade/modules/lib"
	"jade/modules/tasks"
	"log"
)

func main() {
	dir := flag.String("dir", "", "Base dir")
	month := flag.String("m", "", "Month to get report")
	storage := flag.String("t", "", "Save data Type:mysql or clickhouse")
	flag.Parse()

	dbConnections, config, clickhouse, err := lib.GetConfig(*dir)
	c := cron.New()

	for _, task := range config.Tasks {
		log.Printf("Start scheduling task %s", task.Name)

		switch task.Script {
		case "Prepaid":
			_, err = c.AddFunc(task.Cron, func() {
				tasks.Prepaid(dbConnections, *month, clickhouse, *storage)
			})
		case "performTask2":
			_, err = c.AddFunc(task.Cron, func() {
				tasks.PerformTask2(dbConnections)
			})
		default:
			log.Printf("Unknown script for task %s", task.Name)
			continue
		}

		if err != nil {
			log.Printf("Error scheduling task %s: %v", task.Name, err)
		}
	}

	c.Start()
	defer lib.CloseAllDatabases(dbConnections)

	select {} // Block forever
}
